using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Renci.SshNet.Sftp;
using ssh;
using dotnet;
using LitJson;
using wspace;

namespace command
{
    public class Diff : Cmd
    {
        public Diff()
        {
            this.Command = "diff";
            this.NeedArg = false;
            this.IsWorkAreaCmd = true;
            this.Discript = "compare with the server repository.";
            this.Format = "diff";
            this.Function = Func;
        }

        private void Func(string[] args)
        {
            if (args.Length != 1)
            {
                Utils.ErrorFormat(this.Format);
                return;
            }
            ServerInfo sinfo = ServerInfo.GetByConfig(Utils.GetTopCfgPath() + Utils.GetSlash() + Configs.ConfigName);
            if (sinfo == null) return;
            sinfo.dir = sinfo.dir.Replace('\\', '/');
            sinfo.dir = sinfo.dir[sinfo.dir.Length - 1] == '/' ? sinfo.dir.Substring(0, sinfo.dir.Length - 1) : sinfo.dir;

            SftpConnect brg = new SftpConnect(sinfo);
            
            if (!brg.Connect())
            {
                return;
            }
            if (!brg.sftp.Exists(sinfo.dir))
            {
                Utils.ErrorLog("The remote folder does not exist.");
                brg.DisConnect();
                return;
            }
            string lpath = Directory.GetParent(this.Data).FullName;
            LocalCmp lcmp = new LocalCmp(brg, sinfo.dir, lpath);
            lcmp.CmpLocalDiff();
            ServerCmp scmp = new ServerCmp(brg, sinfo.dir, lpath);
            scmp.CmpServerDiff(lcmp.LignoreList);
            brg.DisConnect();

            foreach (string smdf in scmp.RmodifyList)
            {
                System.Console.WriteLine("\t[M](R) " + smdf);
            }
            foreach (string lmdf in scmp.LmodifyList)
            {
                System.Console.WriteLine("\t[M](L) " + lmdf);
            }
            foreach (string rnew in scmp.RnewFList)
            {
                System.Console.WriteLine("\t[+](R) " + rnew);
            }

            foreach (string lnew in lcmp.LnewFList)
            {
                System.Console.WriteLine("\t[+](L) " + lnew);
            }
        }
    }
}